//
//  XZSearchVC.m
//  Teecloud
//
//  Created by PC on 2018/11/2.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZSearchVC.h"
#import "XZSearchResultVC.h"
#import "XZSearchCell.h"
#import "XZSearchHeadView.h"
#import "XZSearchData.h"
@interface XZSearchVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) XZSearchData *model;
@end
#define HEADVIEW @"HEADVIEW"
#define IDENTIFIER @"XZSearchCell"
@implementation XZSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"搜索";
    self.view.backgroundColor = [UIColor colorWithHexString:@"CECECE" andAlpha:1.0f];;
    [self getSearchKey];
}
- (void)getSearchKey{
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"searchFrom":@(5)
                          };
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetSearchKey&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSLog(@"%@",responseObject);
                NSDictionary *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                XZSearchData *model = [XZSearchData mj_objectWithKeyValues:dataInfo];
                weakself.model = model;
                [weakself.collectionView reloadData];
            }else{
              
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:self.view];
            }
            
        }else{
            NSLog(@"%@",error);
        }
    }];
}

#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (self.model.LastSearchKeys.count > 0) {
        return 2;
    }
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.model.LastSearchKeys.count > 0) {
        if (section == 0) {
            return self.model.LastSearchKeys.count;
        }else if (section == 1){
            return self.model.HotSearchKeys.count;
        }
    }
    return self.model.HotSearchKeys.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    kWeakObject(self)
    XZSearchCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER forIndexPath:indexPath];
    cell.contentView.backgroundColor = kWhiteColor;
    if (self.model.LastSearchKeys.count > 0) {
        if (indexPath.section == 0) {
            NSString *key = self.model.LastSearchKeys[indexPath.row];
            cell.searchKey.text = key;
        }else if (indexPath.section == 1){
            NSString *key = self.model.HotSearchKeys[indexPath.row];
            cell.searchKey.text = key;
        }
    }else{
        NSString *key = self.model.HotSearchKeys[indexPath.row];
        cell.searchKey.text = key;
    }
    cell.searchBlock = ^(NSString * _Nonnull key) {
        XZSearchResultVC *searchResultVC = [[XZSearchResultVC alloc]init];
        searchResultVC.key =key;
        [weakself.navigationController pushViewController:searchResultVC animated:true];
    };
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XZSearchHeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADVIEW forIndexPath:indexPath];
        if (self.model.LastSearchKeys.count>0) {
            if (indexPath.section == 0) {
                headView.headTitleLabel.text = @"历史搜索";
            }else if (indexPath.section == 1){
                headView.headTitleLabel.text = @"热门搜索";
            }
            
        }else{
            headView.headTitleLabel.text = @"热门搜索";
        }
        return headView;
    }
    return nil;
}
#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreenWidth/2, 40);
}


#pragma mark - Override -
- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * flayOut = [[UICollectionViewFlowLayout alloc]init];
        flayOut.minimumLineSpacing = 0;
        flayOut.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 320) collectionViewLayout:flayOut];
        _collectionView.backgroundColor = kWhiteColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.showsVerticalScrollIndicator = false;
        _collectionView.showsHorizontalScrollIndicator = false;
        //cell 头部注册
        [_collectionView registerNib:[UINib nibWithNibName:@"XZSearchCell" bundle:nil] forCellWithReuseIdentifier:IDENTIFIER];
        [_collectionView registerNib:[UINib nibWithNibName:@"XZSearchHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADVIEW];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}



@end
